package org.chen.spring.cloud.finchley.zuul.groovy

import com.netflix.zuul.ZuulFilter
import com.netflix.zuul.context.RequestContext
import com.netflix.zuul.exception.ZuulException
import org.apache.catalina.servlet4preview.http.HttpServletRequest
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE

/**
 * @author chensj* @date 2020-02-09 08:14
 */
class GroovyFilter extends ZuulFilter {

    private static final Logger log = LoggerFactory.getLogger(GroovyFilter.class);

    @Override
    String filterType() {
        return PRE_TYPE
    }

    @Override
    int filterOrder() {
        return 10
    }

    @Override
    boolean shouldFilter() {
        return true
    }

    @Override
    Object run() throws ZuulException {
        log.info("这是一个Groovy Filter")
        HttpServletRequest request = RequestContext.currentContext.request as HttpServletRequest
        Iterator headerIt = request.getHeaderNames().iterator()
        while (headerIt.hasNext()) {
            String name = (String) headerIt.next()
            String value = request.getHeader(name)
            println("header: " + name + ":" + value)
        }
        println("This is Groovy Filter!Modify")
        return null
    }
}
